﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.UI;
using TaxBiz.SysConfig;
using TaxBiz.House;
using TaxBiz.User;
using TaxEntity.House;

namespace Admin.House
{
    public partial class HouseModify : BaseCommon.BasePage
    {
        protected string id = string.Empty;
        protected DataTable dtDepartmet = null;
        protected DataTable dtXiangzhen = null;
        protected DataTable dtJiedao = null;
        protected DataTable dtDiduan = null;
        protected DataTable dtManager = null;
        protected HouseInfoBiz hib = new HouseInfoBiz();
        protected HouseInfoEntity hie = new HouseInfoEntity();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            id = PageContext.GetRequest("id");
            if (string.IsNullOrEmpty(id))
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            long iId = 0;
            long.TryParse(id, out iId);
            if (iId <= 0)
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            hie = hib.GetEntityById(iId);
            if (hie == null)
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            string action = PageContext.GetRequest("action");
            if (!string.IsNullOrEmpty(action))
            {
                this.DoAction(action, iId);
            }
            else
            {
                //初始化选择控件
                dtDepartmet = new DepartmentBiz().GetList();
                dtManager = new UserBiz().GetList(hie.DepartmentID);
                dtXiangzhen = new XiangzhenBiz().GetList();
                if (hie.XiangzhenID.HasValue && hie.XiangzhenID.Value > 0)
                {
                    dtJiedao = new JiedaoBiz().GetList(hie.XiangzhenID.Value);
                }
                //if (hie.JiedaoID.HasValue && hie.JiedaoID.Value > 0)
                //{
                //    dtDiduan = new DiDuanBiz().GetList(hie.JiedaoID.Value);
                //}
                dtDiduan = new DiDuanBiz().GetList();
            }
        }

        protected bool DoAction(string action, long id)
        {
            hie.Id = id;
            hie.OptUserID = UserLogin.UserId;
            hie.ModifyTime = DateTime.Now;
            hie.Name = PageContext.GetRequest("txtName", 20);
            hie.IDCardNum = PageContext.GetRequest("txtIDCardNum", 25);
            hie.DepartmentID = PageContext.GetRequestInt("selDepart").Value;
            hie.ManagerID = PageContext.GetRequestInt("selManager").Value;
            //hie.CertificateID = PageContext.GetRequest("txtCertificateID", 50);
            string strCertificateDate = PageContext.GetRequest("txtCertificateDate");
            if (!string.IsNullOrEmpty(strCertificateDate))
            {
                hie.CertificateDate = DateTime.Parse(strCertificateDate);
            }
            if (PageContext.GetRequestInt("txtYearLimit").HasValue)
            {
                hie.YearLimit = PageContext.GetRequestInt("txtYearLimit").Value;
            }
            hie.BuildingNum = PageContext.GetRequest("txtBuildingNum", 50);
            //乡镇
            string strXiangzhen = PageContext.GetRequest("selXiangzhen");
            int xiangzhenID = 0;
            int.TryParse(strXiangzhen, out xiangzhenID);
            hie.XiangzhenID = xiangzhenID;
            //街道
            string strJiedao = PageContext.GetRequest("selJiedao");
            int JiedaoID = 0;
            int.TryParse(strJiedao, out JiedaoID);
            hie.JiedaoID = JiedaoID;
            hie.DiduanID = PageContext.GetRequestInt("selDiduan");
            hie.TudiArea = PageContext.GetRequestDecimal("txtTudiArea");
            hie.FangchanArea = PageContext.GetRequestDecimal("txtFangchanArea");
            hie.OriginalPrice = PageContext.GetRequestDecimal("txtOriginalPrice");
            hie.RemainPrice = PageContext.GetRequestDecimal("txtRemainPrice");
            hie.HouseType = PageContext.GetRequest("selHouseType", 10);
            hie.MobileCode = PageContext.GetRequest("txtMobileCode", 50);
            hie.Telephone = PageContext.GetRequest("txtTelephone", 50);
            hie.Address = PageContext.GetRequest("txtAddress", 100);

            bool isSucc = hib.Update(hie, UserLogin);
            if (!isSucc)
            {
                Jscript.Alert("修改失败");
                return false;
            }

            Jscript.AlertAndRedirect("修改成功", "HouseModify.aspx?id="+id);
            return true;
        }
    }
}